<template>
  <div>
    <!-- 登录功能 -->
    <div class="login">
      <div class="bg"></div>
      <div class="music-main">
        <div class="logo">
          <svg>
            <use xlink:href="#icon-wangyiyunyinle-"></use>
          </svg>
          <div class="form">
            <div class="item">
              <span class="iconfont icon-yonghu-yuan"></span>
              <input type="text" placeholder="手机号码" v-model="phone" />
            </div>
            <div class="item">
              <span class="icon iconfont icon-mima"></span>
              <input type="password" placeholder="密码" v-model="password" />
            </div>
            <div class="item">
              <span class="iconfont icon-yanzhengma"></span>
              <input type="text" placeholder="验证码" v-model="code" />
              <button class="getCode" @click="getCode">发送验证码</button>
            </div>
            <div class="item">
              <button class="goin" @click="submit">立即登录</button>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { getCaptchaSent,getLoginCellphone } from "../../api/login";
import { Toast } from "vant";
export default {
  created() {},
  data() {
    return {
      phone: "",
      password: "",
      code: "",
    };
  },
  methods: {
    getCode() {
      let verifyPhone = /^(?:(?:\+|00)86)?1(?:(?:3[\d])|(?:4[5-79])|(?:5[0-35-9])|(?:6[5-7])|(?:7[0-8])|(?:8[\d])|(?:9[189]))\d{8}$/;
      if (!verifyPhone.test(this.phone)) {
        Toast("请输入正确手机号码");
        return;
      }
      //请求验证码
      getCaptchaSent({ phone: this.phone }).then((data) => {
        console.log(data);
        if (data.data) {
          Toast("验证码发送成功");
        } else {
          Toast("验证码发送失败");
        }
      });
    },

    submit() {
      let reg = /^[0-9]{4}$/;
      if (!reg.test(this.code)) {
        Toast("验证码错误");
        return;
      }

      //验证手机号
      let verifyPhone = /^(?:(?:\+|00)86)?1(?:(?:3[\d])|(?:4[5-79])|(?:5[0-35-9])|(?:6[5-7])|(?:7[0-8])|(?:8[\d])|(?:9[189]))\d{8}$/;
      if (!verifyPhone.test(this.phone)) {
        Toast("请输入正确手机号码");
        return;
      }
      
      //验证密码
      let verifyPassword = /^[a-zA-Z]\w{5,17}$/
      if (!verifyPassword.test(this.password)) {
        Toast("请输入正确信息");
        return;
      }     

      //验证验证码
      // getCaptchaVerifyt({ "phone": this.phone, "captcha": this.code }).then((data) => {
      //     console.log(data);
      //     if (data.data) {
            // 获取用户名密码验证
            getLoginCellphone({
              phone:this.phone,
              password:this.password,
              }).then(info=>{
              console.log(info)
               Toast("登录成功");
               window.localStorage.setItem("login",info.cookie)
               window.localStorage.setItem("token",info.token)
                window.localStorage.setItem("uid",info.account.id)
               this.$router.push("/usermessage").catch(()=>{})
              //  this.$router.push("/")
            })
        //   } else {
        //     Toast("请填写正确信息");
        //   }
        // }).catch(() => {
        //   Toast("请填写正确信息");
        // });
    },
  },
};
</script>

<style lang="less">
.login {
  width: 100%;
  height: 100%;
  .bg {
    width: 100%;
    height: 100%;
    position: fixed;
    top: 0;
    left: 0;
    background-color: #dc2c1f;
    z-index: -1;
  }
  .music-main {
    position: absolute;
    top: 40%;
    left: 50%;
    transform: translate(-50%, -40%);
    text-align: center;
    .logo {
      width: 100%;
      svg {
        fill: #fff;
        overflow: hidden;
        position: relative;
        bottom: -18px;
      }
      .form {
        .item {
          margin-top: 10px;
          position: relative;
          input,
          .goin {
            width: 100%;
            box-sizing: border-box;
            padding-left: 32px;
            border: none;
            height: 37px;
            line-height: 37px;
            border-radius: 10px;
            font-size: 14px;
          }
          span {
            position: absolute;
            left: 5px;
            top: 50%;
            transform: translateY(-50%);
            font-size: 22px;
            color: #afabab;
          }
          .icon {
            font-size: 20px;
          }
          .getCode {
            // width: 20%;
            // height: 28px;
            padding-left: 12px;
            border: none;
            opacity: 0.6;
            border-left: 1px solid #4f4f4f;
            background-color: #fff;
            position: absolute;
            right: 15px;
            top: 50%;
            transform: translateY(-50%);
            font-size: 14px;
          }
          .goin {
            color: #dc2c1f;
            padding-left: 0;
          }
        }
      }
    }
  }
}
</style>

